<template>
  <a-date-picker
    dropdownClassName="j-date-picker"
    :disabled="disabled || readOnly"
    :placeholder="placeholder"
    @change="handleDateChange"
    @panelChange="panelChange"
    :open="isopen"
    @openChange="openChange"
    :value="momVal"
    :mode="mode"
    :showTime="showTime"
    :format="dateFormat"
    :getCalendarContainer="getCalendarContainer"
    v-bind="$attrs"
  />
</template>
<script>
import moment from 'moment'
export default {
  name: 'JDate',
  props: {
    placeholder: {
      type: String,
      default: '',
      required: false,
    },
    mode: {
      type: String,
      default: 'date',
      required: false,
    },
    value: {
      type: String,
      required: false,
    },
    dateFormat: {
      type: String,
      default: 'YYYY-MM-DD',
      required: false,
    },
    //此属性可以被废弃了
    triggerChange: {
      type: Boolean,
      required: false,
      default: false,
    },
    readOnly: {
      type: Boolean,
      required: false,
      default: false,
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
    showTime: {
      type: Boolean,
      required: false,
      default: false,
    },
    getCalendarContainer: {
      type: Function,
      default: (node) => node.parentNode,
    },
  },
  data() {
    let dateStr = this.value
    return {
      isopen: false,
      decorator: '',
      momVal: !dateStr ? null : moment(dateStr, this.dateFormat),
    }
  },
  watch: {
    value(val) {
      console.log("时间设置",val)
      if (!val) {
        this.momVal = null
      } else {
        this.momVal = moment(val, this.dateFormat)
      }
    },
  },
  methods: {
    moment,
    handleDateChange(mom, dateStr) {
      console.log('handleDateChange', dateStr)
      this.momVal=mom
      this.$emit('change', dateStr)
    },
    // 使用openChange事件控制日期下拉面板显示与收起
    openChange(isopen) {
      if (isopen) {
        this.isopen = true
      } else {
        this.isopen = false
      }
    },
    panelChange(mom) {
      this.momVal=mom
      this.$emit('change', moment(mom).format(this.dateFormat))
      this.isopen = false
    },
  },
  //2.2新增 在组件内定义 指定父组件调用时候的传值属性和事件类型 这个牛逼
  model: {
    prop: 'value',
    event: 'change',
  },
}
</script>
